global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/18_firm_binary_table_figures", replace

*Program to plot regressions
cap program drop plot_est
program define plot_est 
args var ytitle ylabel

	*Save estimates
	use "$output/explosure_`var'_estimates.dta", clear
	tab event_time 
	label define time_lab -12 "Apr 2015" -11 "May 2015" -10 "Jun 2015" ///
		-9 "Jul 2015" -8 "Aug 2015" -7 "Sep 2015" -6 "Oct 2015" -5 "Nov 2015" -4 "Dec 2015" ///
		-3 "Jan 2016" -2 "Feb 2016" -1 "Mar 2016" 0 "Apr 2016" 1 "May 2016" 2 "Jun 2016" ///
		3 "Jul 2016" 4 "Aug 2016" 5 "Sep 2016" 6 "Oct 2016" 7 "Nov 2016" 8 "Dec 2016" ///
		9 "Jan 2017" 10 "Feb 2017" 11 "Mar 2017" 12 "Apr 2017" 13 "May 2017" 14 "Jun 2017" ///
		15 "Jul 2017" 16 "Aug 2017" 17 "Sep 2017" 18 "Oct 2017" 19 "Nov 2017" 20 "Dec 2017" ///
		21 "Jan 2018" 22 "Feb 2018" 23 "Mar 2018" 24 "Apr 2018" 25 "May 2018", replace
		label values event_time time_lab 
		
	*Plot estimates
	keep if spec=="Basic"| spec=="State_FE" | spec=="NAICS_FE"
	graph twoway (connect beta event_time if spec=="Basic", lcolor(black) mcolor(black) msize(medium)) (rcap ub lb event_time if spec=="Basic", lcolor(black)) ///
		(connect beta2 event_time if spec=="Basic", lcolor(maroon) mcolor(maroon) msize(medium)) (rcap ub2 lb2 event_time if spec=="Basic", lcolor(maroon)), ///
		xline(1 8) xtitle("Month") xlabel(-20(5)20) xlabel(-12(3)25, valuelabel angle(45))  ///
		yline(0) ytitle(`ytitle') ylabel(`ylabel') legend(order(1 "High" 3 "Medium") subtitle("Exposure") ring(0))
	graph export "$graphs/explosure_`var'.eps", replace
	graph export "$graphs/explosure_`var'.jpg", replace	
	
	if "`var'"=="log_n" {
	graph twoway (connect beta event_time if spec=="Basic", lcolor(black) mcolor(black) msize(medium)) (rcap ub lb event_time if spec=="Basic", lcolor(black)) ///
		(connect beta event_time if spec=="State_FE", lcolor(maroon) mcolor(maroon) msize(medium)) (rcap ub lb event_time if spec=="State_FE", lcolor(maroon)), ///
		xline(1 8) xtitle("Month") xlabel(-20(5)20) xlabel(-12(3)25, valuelabel angle(45)) ///
		yline(0) ytitle(`ytitle') ylabel(`ylabel') legend(order(1 "Firm + Month FE" 3 "Firm + Month-State FE") col(1) subtitle("Fixed Effects") ring(0))
	graph export "$graphs/explosure_`var'_fe.eps", replace
	graph export "$graphs/explosure_`var'_fe.jpg", replace	
	}

	/*graph twoway (connect beta event_time if spec=="State_FE", lcolor(black) mcolor(black)) (rcap ub lb event_time if spec=="State_FE", lcolor(black)) ///
		(connect beta2 event_time if spec=="State_FE", lcolor(maroon) mcolor(maroon)) (rcap ub2 lb2 event_time if spec=="State_FE", lcolor(maroon)), ///
		xline(1 8) xtitle("Month") xlabel(-20(5)20) xlabel(-12(3)25, valuelabel angle(45)) ///
		yline(0) ytitle(`ytitle') ylabel(`ylabel') legend(order(1 "High" 3 "Medium") col(1) subtitle("Exposure") ring(0))
	graph export "$graphs/explosure_`var'_fe2.eps", replace
	graph export "$graphs/explosure_`var'_fe2.jpg", replace	*/
end 
	
	plot_est log_n "Log Number of Workers" -0.08(0.02)0.04
	plot_est log_bill_sal "Log Avg. Wage Bill of Salaried Workers" -0.04(0.02)0.06
	plot_est log_bill_hr "Log Avg. Wage Bill of Hourly Workers" -0.04(0.02)0.06


**************************************
*Output Table
**************************************
cap program drop save_est
program define save_est 
args var start

	*Save estimates
	use  "$output/explosure_`var'_estimates.dta", clear
	keep if se!=. 
	
	local index = `start'
	foreach spec in Basic State Naics {
		
		*Regression Coefficients
		sum beta if spec=="`spec'1p66"
		global b`index'_`var'=round(r(mean), 0.0001)
		sum se if spec=="`spec'1p66"
		global se`index'_`var'=round(r(mean),0.0001)

		*May 2016 - Nov 2016 coefficients
		sum beta if spec=="`spec'2p66"
		global c`index'_`var'=round(r(mean), 0.0001)
		sum se if spec=="`spec'2p66"
		global ce`index'_`var'=round(r(mean),0.0001)

		*Baseline characteristics
		sum obs if spec=="`spec'1p66"
		global n`index'_`var'=string(r(mean), "%10.0fc")
		
		local ++index
	}
end 

	save_est log_n 1
	save_est log_bill_sal 1
	save_est log_bill_hr 1
	
*Appendix Table 1
	capture erase "$graphs/tableA1_firm_binary_update.tex"
	file open fh3 using "$graphs/tableA1_firm_binary_update.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) \\" _n ///
		"\midrule" _n ///
		"\vspace{0.2cm}" _n ///
		"Log Employment \\ " _n ///
		"\hspace{1cm} May - Nov 2016 && $c1_log_n && $c2_log_n && $c3_log_n   \\" _n ///
		"\vspace{0.4cm}" _n ///
		" && ($ce1_log_n) && ($ce2_log_n) && ($ce3_log_n) \\ " _n ///
		"\hspace{1cm} Post Dec 2016 && $b1_log_n && $b2_log_n && $b3_log_n   \\" _n ///
		" && ($se1_log_n) && ($se2_log_n) && ($se3_log_n) \\ \\" _n ///
		"\vspace{0.2cm}" _n ///
		"Log Avg. Salaried Wage Bill \\ " _n ///
		"\hspace{1cm} May - Nov 2016 && $c1_log_bill_sal && $c2_log_bill_sal && $c3_log_bill_sal   \\" _n ///
		"\vspace{0.4cm}" _n ///
		" && ($ce1_log_bill_sal) && ($ce2_log_bill_sal) && ($ce3_log_bill_sal) \\" _n ///
		"\hspace{1cm} Post Dec 2016 && $b1_log_bill_sal && $b2_log_bill_sal && $b3_log_bill_sal \\" _n ///
		" && ($se1_log_bill_sal) && ($se2_log_bill_sal) && ($se3_log_bill_sal)  \\ \\" _n ///
		"\vspace{0.2cm}" _n ///
		"Log Avg. Hourly Wage Bill \\ " _n ///
		"\hspace{1cm} May - Nov 2016 && $c1_log_bill_hr && $c2_log_bill_hr && $c3_log_bill_hr   \\" _n ///
		"\vspace{0.4cm}" _n ///
		" && ($ce1_log_bill_hr) && ($ce2_log_bill_hr) && ($ce3_log_bill_hr) \\" _n ///
		"\hspace{1cm} Post Dec 2016 && $b1_log_bill_hr && $b2_log_bill_hr && $b3_log_bill_hr \\" _n ///
		" && ($se1_log_bill_hr) && ($se2_log_bill_hr) && ($se3_log_bill_hr)  \\" _n ///
		"\midrule" _n ///
		"Firm FE 		  && Y && Y && Y   \\" _n ///
		"Month FE 		  && Y && Y && Y  \\" _n ///
		"State-Month FE   && - && Y && Y \\" _n ///
		"State-Month-NAICS FE 	&& - && - && Y \\" _n ///
		"N && $n1_log_n && $n2_log_n && $n3_log_n  \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3 
	

**************************************
*Output Table for medium exposed firms
**************************************
cap program drop save_est
program define save_est 
args var start

	*Save estimates
	use  "$output/explosure_`var'_estimates.dta", clear
	keep if se!=. 
	
	local index = `start'
	foreach spec in Basic State Naics {
		
		*Regression Coefficients
		sum beta if spec=="`spec'1p33"
		global b`index'_`var'=round(r(mean), 0.0001)
		sum se if spec=="`spec'1p33"
		global se`index'_`var'=round(r(mean),0.0001)

		*May 2016 - Nov 2016 coefficients
		sum beta if spec=="`spec'2p33"
		global c`index'_`var'=round(r(mean), 0.0001)
		sum se if spec=="`spec'2p33"
		global ce`index'_`var'=round(r(mean),0.0001)
		
		*Baseline characteristics
		sum obs if spec=="`spec'1p33"
		global n`index'_`var'=string(r(mean), "%10.0fc")
		
		local ++index
	}
end 

	save_est log_n 1
	save_est log_bill_sal 1
	save_est log_bill_hr 1
	
*Appendix Table 1
	capture erase "$graphs/tableA1_firm_binary_med_update.tex"
	file open fh3 using "$graphs/tableA1_firm_binary_med_update.tex", write replace

	file write fh3 ///
		"\begin{tabular}{@{\hskip\tabcolsep\extracolsep\fill} l cc cc cc}" _n ///
		"\toprule" _n ///
		"&& (1) && (2) && (3) \\" _n ///
		"\midrule" _n ///
		"\vspace{0.2cm}" _n ///
		"Log Employment \\ " _n ///
		"\hspace{1cm} May - Nov 2016 && $c1_log_n && $c2_log_n && $c3_log_n   \\" _n ///
		"\vspace{0.4cm}" _n ///
		" && ($ce1_log_n) && ($ce2_log_n) && ($ce3_log_n) \\ " _n ///
		"\hspace{1cm} Post Dec 2016 && $b1_log_n && $b2_log_n && $b3_log_n   \\" _n ///
		" && ($se1_log_n) && ($se2_log_n) && ($se3_log_n) \\ \\" _n ///
		"\vspace{0.2cm}" _n ///
		"Log Avg. Salaried Wage Bill \\ " _n ///
		"\hspace{1cm} May - Nov 2016 && $c1_log_bill_sal && $c2_log_bill_sal && $c3_log_bill_sal   \\" _n ///
		"\vspace{0.4cm}" _n ///
		" && ($ce1_log_bill_sal) && ($ce2_log_bill_sal) && ($ce3_log_bill_sal) \\" _n ///
		"\hspace{1cm} Post Dec 2016 && $b1_log_bill_sal && $b2_log_bill_sal && $b3_log_bill_sal \\" _n ///
		" && ($se1_log_bill_sal) && ($se2_log_bill_sal) && ($se3_log_bill_sal)  \\ \\" _n ///
		"\vspace{0.2cm}" _n ///
		"Log Avg. Hourly Wage Bill \\ " _n ///
		"\hspace{1cm} May - Nov 2016 && $c1_log_bill_hr && $c2_log_bill_hr && $c3_log_bill_hr   \\" _n ///
		"\vspace{0.4cm}" _n ///
		" && ($ce1_log_bill_hr) && ($ce2_log_bill_hr) && ($ce3_log_bill_hr) \\" _n ///
		"\hspace{1cm} Post Dec 2016 && $b1_log_bill_hr && $b2_log_bill_hr && $b3_log_bill_hr \\" _n ///
		" && ($se1_log_bill_hr) && ($se2_log_bill_hr) && ($se3_log_bill_hr)  \\" _n ///
		"\midrule" _n ///
		"Firm FE 		  && Y && Y && Y   \\" _n ///
		"Month FE 		  && Y && Y && Y  \\" _n ///
		"State-Month FE   && - && Y && Y \\" _n ///
		"State-Month-NAICS FE 	&& - && - && Y \\" _n ///
		"N && $n1_log_n && $n2_log_n && $n3_log_n  \\" _n ///
		"\bottomrule" _n ///
		"\end{tabular}" _n
		
	file close fh3 
	macro drop fh3  
	

log close
